<template>
  <div>
    <div class="app-container">
      <el-container>
        <el-header style="height: 150px;">
          <el-form :inline="true" style="padding-top: 20px;">
            <el-form-item label="车型编号:" label-width="100px">
              <el-input v-model="queryparams.id" placeholder="请输入车型编号" style="width:280px" />
            </el-form-item>
            <el-form-item label="应载重量:" label-width="100px">
              <el-select v-model="queryparams.allowableLoad" placeholder="请选择应载体重" style="width:280px">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.name"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="应载体积:" label-width="100px">
              <el-select v-model="queryparams.allowableVolume" placeholder="请选择应载体积" style="width:280px">
                <el-option
                  v-for="item in options1"
                  :key="item.value"
                  :label="item.name"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="车辆类型:" label-width="100px">
              <el-input v-model="queryparams.name" placeholder="请输入车辆类型" style="width:280px" />
            </el-form-item>
            <el-button type="danger" style="width:100px" @click="search">搜索</el-button>
            <el-button type="danger" style="width:100px" plain @click="reset">重置</el-button>
          </el-form>
        </el-header>
        <el-main>
          <div>
            <el-button type="danger" style="width:150px" @click="addcar">新增车型</el-button>
            <template>
              <el-table :data="list">
                <el-table-column label="序号" width="100">
                  <template v-slot="{ $index }">
                    {{ $index + 1 }}
                  </template>
                </el-table-column>
                <el-table-column prop="id" label="车型编号" width="200" />
                <el-table-column prop="name" label="车辆类型" width="150" />
                <el-table-column prop="num" label="车型数量" width="100" />
                <el-table-column prop="allowableLoad" label="应载重量(吨)" width="100" />
                <el-table-column prop="allowableVolume" label="应载体积(立方米)" width="150" />
                <el-table-column prop="measureLong" label="长(米)" width="100" />
                <el-table-column prop="measureWidth" label="宽(米)" width="100" />
                <el-table-column prop="measureHigh" label="高(米)" width="100" />
                <el-table-column label="操作">
                  <template v-slot="{row}">
                    <el-button type="text" size="small" @click="updatacar(row.id)">编辑</el-button>
                    <el-button type="text" size="small" @click="delcar(row.id)">删除</el-button>
                  </template>
                </el-table-column>
              </el-table>
            </template>
          </div>
        </el-main>
        <!-- 分页 -->
        <div class="block">
          <el-pagination
            :current-page="queryparams.page"
            :page-sizes="[10, 20, 30, 50]"
            :page-size="queryparams.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="counts"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          >
            />
          </el-pagination>
        </div>
      </el-container>
    </div>
    <!-- 放置一个弹层 用来编辑新增车型 -->
    <el-dialog :title="`${showText}车型`" :visible.sync="isvisible">
      <!-- 表单 -->
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="车辆名称" prop="name">
          <el-input v-model="form.name" style="width:90%" placeholder="请输入车辆名称" />
        </el-form-item>
        <el-form-item label="应载重量" prop="allowableLoad">
          <el-input v-model="form.allowableLoad" style="width:90%" placeholder="请输入应载重量">
            <i slot="suffix" style="font-style:normal;margin-right: 10px;color:black">吨</i>
          </el-input>
        </el-form-item>
        <el-form-item label="应载体积" prop="allowableVolume">
          <el-input v-model="form.allowableVolume" style="width:90%" placeholder="请输入应载体积">
            <i slot="suffix" style="font-style:normal;margin-right: 10px;color:black">立方</i>
          </el-input>
        </el-form-item>
        <el-form-item label="长" prop="measureLong">
          <el-input v-model="form.measureLong" style="width:90%">
            <i slot="suffix" style="font-style:normal;margin-right: 10px;color:black">米</i>
          </el-input>
        </el-form-item>
        <el-form-item label="宽" prop="measureWidth">
          <el-input v-model="form.measureWidth" style="width:90%">
            <i slot="suffix" style="font-style:normal;margin-right: 10px;color:black">米</i>
          </el-input>
        </el-form-item>
        <el-form-item label="高" prop="measureHigh">
          <el-input v-model="form.measureHigh" style="width:90%" type="number">
            <i slot="suffix" style="font-style:normal;margin-right: 10px;color:black">米</i>
          </el-input>
        </el-form-item>
      </el-form>
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" type="primary" @click="btnOK">确定</el-button>
          <el-button size="small" @click="btnCancel">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>

import { getcarManagement, addcarManagement, updatacarManagement, getcarManagementdatil, delcarManagementdatil, gettypes } from '@/api/car'
export default {
  data() {
    return {
      options: [
        {
          value: 'RANGE_LEVEL_1',
          name: '<1.8（吨）'
        },
        {
          value: 'RANGE_LEVEL_2',
          name: '1.8-6（吨）'
        },
        {
          value: 'RANGE_LEVEL_3',
          name: '6-14（吨）'
        },
        {
          value: 'RANGE_LEVEL_4',
          name: '14-30（吨）'
        },
        {
          value: 'RANGE_LEVEL_5',
          name: '30-50（吨）'
        },
        {
          value: 'RANGE_LEVEL_6',
          name: '50-100（吨）'
        },
        {
          value: 'RANGE_LEVEL_7',
          name: '100>（吨）'
        }],
      value: '',
      options1: [
        {
          value: 'RANGE_LEVEL_1',
          name: '<3（m³）'
        },
        {
          value: 'RANGE_LEVEL_2',
          name: '3-5（m³）'
        },
        {
          value: 'RANGE_LEVEL_3',
          name: '5-10（m³）'
        },
        {
          value: 'RANGE_LEVEL_4',
          name: '10-15（m³）'
        },
        {
          value: 'RANGE_LEVEL_5',
          name: '15-30（m³）'
        },
        {
          value: 'RANGE_LEVEL_6',
          name: '30-50（m³）'
        },
        {
          value: 'RANGE_LEVEL_7',
          name: '50-80（m³）'
        },
        {
          value: 'RANGE_LEVEL_8',
          name: '80-150（m³）'
        },
        {
          value: 'RANGE_LEVEL_9',
          name: '150>（m³）'
        }
      ],
      value1: '',
      // 存储查询参数
      queryparams: {
        page: 1,
        pageSize: 10,
        id: '',
        name: '',
        allowableLoad: '',
        allowableVolume: ''
      },
      counts: null,
      total: 0,
      list: [],
      isvisible: false,
      ID: null,
      form: {
        name: '',
        allowableLoad: '',
        allowableVolume: '',
        measureHigh: '0.0',
        measureLong: '0.0',
        measureWidth: '0.0'
      },
      rules: {
        name: [
          { required: true, message: '车辆类型不能为空', trigger: 'blur' }
        ],
        allowableLoad: [
          { required: true, message: '应载重量不能为空', trigger: 'blur' },
          { pattern: /^(?!0)\d{1,2}$|^100$/, message: '只可输入1-100之间的整数,请重新输入', trigger: 'blur' }
        ],
        allowableVolume: [
          { required: true, message: '应载体积不能为空', trigger: 'blur' },
          { pattern: /^(?!0)\d{1,2}$|^100$/, message: '只可输入1-100之间的整数,请重新输入', trigger: 'blur' }
        ],
        measureHigh: [
          { pattern: /^[0-9]+(.[0-9]{1,2})?$/, message: '只能输入数字类型，最多保留两位小数，请重新输入', trigger: 'blur' }
        ],
        measureLong: [
          { pattern: /^[0-9]+(.[0-9]{1,2})?$/, message: '只能输入数字类型，最多保留两位小数，请重新输入', trigger: 'blur' }
        ],
        measureWidth: [
          { pattern: /^[0-9]+(.[0-9]{1,2})?$/, message: '只能输入数字类型，最多保留两位小数，请重新输入', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    showText() {
      return this.form.id ? '编辑' : '新增'
    }
  },
  created() {
    // this.getcarManagement()
    this.gettypes()
  },
  methods: {
    async getcarManagement() {
      const res = await getcarManagement()
      this.list = res
    },
    addcar() {
      this.isvisible = true
    },
    async btnOK() {
      this.$refs.form.validate().then(() => {
        if (this.form.id) {
          return updatacarManagement(this.form)
        } else {
          return addcarManagement(this.form)
        }
      }).then(() => {
        this.form.id ? this.$message.success('编辑成功') : this.$message.success('新增成功')
        this.gettypes()
        this.btnCancel()
        this.isvisible = false
      }).catch(() => {
        if (this.form.name === '' || this.form.allowableLoad === '' || this.form.allowableVolume === '') {
          this.$message.error('*号为必填项！')
        }
      })
    },
    btnCancel() {
      this.$refs.form.resetFields()
      this.isvisible = false
    },
    async updatacar(id) {
      this.isvisible = true
      const res = await getcarManagementdatil(id)
      this.form = res
    },
    async delcar(id) {
      try {
        await this.$confirm('确认要删除车型吗', '确认删除', { confirmButtonText: '确定', cancelButtonText: '取消' })
        await delcarManagementdatil(id)
        this.gettypes()
        this.$message.success('删除成功')
      } catch (err) {
        this.$message(err)
      }
    },
    async gettypes() {
      const res = await gettypes(this.queryparams)
      this.list = res.items
      this.counts = Number(res.counts)
    },
    // 分页
    handleSizeChange(pageSize) {
      this.queryparams.pageSize = pageSize
      this.gettypes()
    },
    handleCurrentChange(page) {
      this.queryparams.page = page
      this.gettypes()
    },
    // 搜索
    async search() {
      this.queryparams.page = 1
      this.queryparams.pageSize = 10
      await this.gettypes(this.queryparams)
    },
    // 重置
    reset() {
      this.queryparams = {
        page: 1,
        pageSize: 10,
        id: '',
        name: '',
        allowableLoad: '',
        allowableVolume: ''
      }
      this.gettypes()
    }
  }
}
</script>

<style>
.block{
  display: flex;
  justify-content: center;
}

.el-header{
    background-color: #fff;
    color: #333;
  }

  .el-main {
    margin-top: 20px;
    background-color: #fff;
    color: #333;
  }

  body > .el-container {
    margin-bottom: 40px;
  }

</style>
